package cn.onein.edu.web.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.SharedActions;
import cn.onein.edu.biz.bean.User;
import cn.onein.edu.biz.bean.UserDailyAction;
import cn.onein.edu.util.DateUtil;
import cn.onein.edu.util.StringUtil;
import cn.onein.edu.web.dao.UserCenterDao;
import cn.onein.edu.web.service.UserCenterService;

/**
 * 用户中心 service接口实现
 * 
 * @author Justin
 * 
 */
@Service
public class UserCenterServiceImpl implements UserCenterService {

	Logger logger = Logger.getLogger(UserCenterServiceImpl.class);
	
	@Autowired
	private UserCenterDao userCenterDao;

	@Override
	public User getUserById(Long id) {
		return userCenterDao.getUserById(id);
	}

	@Override
	public List<UserDailyAction> getDailyAction(Long userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		Date nowDate = new Date();

		Date startDate = DateUtil.getDateBydays(nowDate, -1);
		Date endDate = DateUtil.getDateBydays(nowDate, 1);

		map.put("userId", userId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		
        List<UserDailyAction> list = userCenterDao.getDailyAction(map);
		
		if(list.isEmpty()){
			return list;
		}
		
		if (list.size() < 3) {
		  startDate = DateUtil.getDateBydays(nowDate, -2);
		  map.put("startDate", startDate);
		  map.put("endDate", nowDate);
		  list = userCenterDao.getDailyAction(map);
		}

		return list;
	}

	@Override
	public List<UserDailyAction> getPreDayDailyAction(Long userId, Date currDate) {
		Map<String, Object> map = new HashMap<String, Object>();

		Date startDate = DateUtil.getDateBydays(currDate, -4);
		Date endDate = DateUtil.getDateBydays(currDate, -2);
		map.put("userId", userId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);

		List<UserDailyAction> list = userCenterDao.getDailyAction(map);
		
		if(list.isEmpty()){
			return list;
		}
		
		if (list.size() < 3) {
			endDate = DateUtil.getDateBydays(currDate, 1);
			map.put("endDate", endDate);
			list = userCenterDao.getDailyAction(map);
		}

		return list;
	}

	@Override
	public List<UserDailyAction> getNextDayDailyAction(Long userId,
			Date currDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		Date nowDate = new Date();
		Date toDate = DateUtil.getDateBydays(nowDate, 1);
		Date startDate = DateUtil.getDateBydays(currDate, 2);
		Date endDate = DateUtil.getDateBydays(currDate, 4);
		
		map.put("userId", userId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		
		if(df.format(endDate).compareTo(df.format(toDate)) > 0){
			startDate = DateUtil.getDateBydays(nowDate, -1);
			endDate = DateUtil.getDateBydays(nowDate, 1);
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			return userCenterDao.getDailyActionByDesc(map);
		}

		List<UserDailyAction> list = userCenterDao.getDailyActionByDesc(map);
	
		if (list.size() < 3) {
			startDate = DateUtil.getDateBydays(currDate, -3);
			map.put("startDate", startDate);
			list = userCenterDao.getDailyActionByDesc(map);
		}

		return list;
	}

	@Override
	public List<UserDailyAction> getPreWeekDailyAction(Long userId,
			Date currDate) {
		Map<String, Object> map = new HashMap<String, Object>();

		Date startDate = DateUtil.getDateBydays(currDate, -8);
		Date endDate = DateUtil.getDateBydays(currDate,-6);
		map.put("userId", userId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);

		List<UserDailyAction> list = userCenterDao.getDailyAction(map);
		if(list.isEmpty()){
			return list;
		}
		
		if (list.size() < 3) {
			endDate = DateUtil.getDateBydays(currDate, -3);
			map.put("endDate", endDate);
			list = userCenterDao.getDailyAction(map);
		}

		return list;
	}

	@Override
	public List<UserDailyAction> getNextWeekDailyAction(Long userId,
			Date currDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date nowDate = new Date();

		Date startDate = DateUtil.getDateBydays(currDate, 6);
		Date endDate = DateUtil.getDateBydays(currDate, 8);
		Date toDate = DateUtil.getDateBydays(nowDate, 1);
		map.put("userId", userId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);

		
		if(df.format(endDate).compareTo(df.format(toDate)) > 0){
			startDate = DateUtil.getDateBydays(nowDate, -1);
			endDate = DateUtil.getDateBydays(nowDate, 1);
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			return userCenterDao.getDailyActionByDesc(map);
		}
		
		List<UserDailyAction> list = userCenterDao.getDailyActionByDesc(map);
		
		if (list.size() < 3) {
			startDate = DateUtil.getDateBydays(currDate, 3);
			map.put("startDate", startDate);
			list = userCenterDao.getDailyActionByDesc(map);
		}

		return list;
	}

	@Override
	public void insertBabyDaily(String content, Long userId, String actionDate,String actionResult) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("userId", userId);
		map.put("actionDate", actionDate);
		UserDailyAction action = userCenterDao.getDailyActionByUserIdAndDate(map);
		if(null != action && null != action.getActionResult()){
			if(action.getActionResult() == 1){
				actionResult = "1";
			}
		}
		
		map.clear();
		map.put("babyDaily", content);
		map.put("actionResult", Integer.parseInt(actionResult));
		map.put("userId", userId);
		map.put("actionDate", actionDate);
		
		userCenterDao.insertBabyDaily(map);
	}

	@Override
	public SharedActions insertShareAction(Long userId, String actionDate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date action_date = null;
		String uuid;
		Map<String,Object> map = new HashMap<String,Object>();
		
		try {
			action_date = df.parse(actionDate);
		} catch (ParseException e) {
			logger.error("Format action date error in insertShareAction fuction.",e);
		}
		
		map.put("userId", userId);
		map.put("actionDate", actionDate);
		SharedActions shareAction = userCenterDao.getShareAction(map);
		
		if(null == shareAction){
			shareAction = new SharedActions();
			uuid = StringUtil.getNewUUID();
			shareAction.setUuid(uuid);
			shareAction.setShareActionBaby(Boolean.FALSE);
			
			map.clear();
			map.put("uuid", uuid);
			map.put("userId", userId);
			map.put("actionDate", action_date);
			map.put("shareBabyDaily", Boolean.TRUE);
			userCenterDao.insertShareAction(map);
		}
		
		return shareAction;
	}
	
	public void updateShareAction(Long userId,String actionDate,String shareBabyDaily){
		Map<String,Object> map = new HashMap<String,Object>();
			
		map.put("shareBabyDaily", "1".equals(shareBabyDaily) ? Boolean.TRUE : Boolean.FALSE);
		map.put("userId", userId);
		map.put("actionDate", actionDate);
		
		userCenterDao.updateShareAction(map);
	}

	@Override
	public List<UserDailyAction> getInputDayDailyAction(Long userId,
			Date currDate) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		Date startDate = DateUtil.getDateBydays(currDate, -1);
		Date endDate = DateUtil.getDateBydays(currDate, 1);
		Date toDate = DateUtil.getDateBydays(new Date(), 1);
		
		map.put("userId", userId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		
		if(df.format(endDate).compareTo(df.format(toDate)) >= 0){
			startDate = DateUtil.getDateBydays(new Date(), -1);
			endDate = DateUtil.getDateBydays(new Date(), 1);
			map.put("startDate", startDate);
			map.put("endDate", endDate);
			return userCenterDao.getDailyAction(map);
		}

		List<UserDailyAction> list = userCenterDao.getDailyAction(map);
		
		if (list.size() < 3) {
			 endDate = DateUtil.getDateBydays(currDate, 2);
			 map.put("startDate", currDate);
			 map.put("endDate", endDate);
			 list = userCenterDao.getDailyAction(map);
		}

		return list;
	}

	@Override
	public UserDailyAction getDailyAction(Long userId, String actionDate) {
		Map<String,Object> map = new HashMap<String,Object>();
		
		map.put("userId", userId);
		map.put("actionDate", actionDate);
		
		UserDailyAction action = userCenterDao.getDailyActionByUserIdAndDate(map);
		
		return action ;
	}

	@Override
	public SharedActions getShareActionByUUID(String uuid) {
		return userCenterDao.getShareActionByUUID(uuid);
	}

}
